package com.zys.el.time.advice.controller;

import com.zys.el.time.advice.service.CommentAdviceService;
import com.zys.el.time.advice.vo.CommentAdviceVo;
import com.zys.el.time.common.Result;
import com.zys.el.time.dto.ScrollPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("advice")
@Api(tags = "收信箱接口")
public class AdviceController {
    @Resource
    private CommentAdviceService commentAdviceService;

    @GetMapping("page")
    @ApiOperation("获取收信箱分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true),
            @ApiImplicitParam(name = "max", value = "上一页的最小值", required = true),
            @ApiImplicitParam(name = "offset", value = "上一页最小值的数量", required = true),
            @ApiImplicitParam(name = "size", value = "页大小", required = true)
    })
    public Result page(@RequestParam("userId") String userId,
                       @RequestParam("max") String max,
                       @RequestParam("offset") Integer offset,
                       @RequestParam("size") Integer size) {
        if (StringUtils.isBlank(userId) || StringUtils.isBlank(max) || offset == null || size == null)
            return Result.fail("缺少参数");

        ScrollPage<CommentAdviceVo> scrollPage = this.commentAdviceService.getPage(Long.valueOf(userId), Long.valueOf(max), offset, size);

        return Result.ok(scrollPage);

    }

    @GetMapping("read")
    public Result alreadyRead(@RequestParam("userId") String userId){
        if(StringUtils.isBlank(userId)) return Result.fail("参数有误");
        this.commentAdviceService.alreadyRead(Long.valueOf(userId));
        return Result.ok("已读成功");
    }

    @GetMapping("clear")
    public Result clear(@RequestParam("userId") String userId){
        if(StringUtils.isBlank(userId)) return Result.fail("参数有误");
        this.commentAdviceService.clear(Long.valueOf(userId));
        return Result.ok("清空成功");
    }


}
